package org.malred
package lecture.part1basics

object expressions extends App {
  val x = 1 + 2 // 1+2 -> expression
  println(x)

  // + - * / & | ^ << >> >>> (right shift with zero extension)
  println(2 + 3 * 4)

  // == != > >= < <=
  println(1 == x)

  // ! && ||
  println(!(1 == x))

  var aVariable = 2
  aVariable += 3 // also works with -= += *= /= ... side effects
  println(aVariable)

  // Instructions (DO) vs Expressions (VALUE)

  // if expression
  val aCondition = true
  val aConditionValue = if (aCondition) 5 else 3
  println(aConditionValue)
  println(if (aCondition) 5 else 3)
  println(1 + 3)

  // scala 也有循环, 但是不建议使用, 容易带来副作用
  // never write this action!
  var i = 0
  val aWhile = while (i < 10) {
    //  while (i < 10) {
    println(i)
    i += 1
  }
  println(aWhile) // () -> 副作用返回空值

  // EVERYTHING in Scala is an Expression!
  val aWeirdValue = (aVariable = 3) // Unit === void
  println(aWeirdValue) // () -> 副作用返回空值

  // side effects: println(), whiles, reassigning

  // code blocks
  val aCodeBlock = {
    val y = 2
    val z = y + 1

    // return value
    if (z > 2) "hello" else "goodbye"
  }
  println(aCodeBlock) // hello

  //  println(z + 1) // 无法解析符号 z

  // 1. difference between "hello world" vs println("hello world")?
  // "" return string, println() return () Unit

  // 2.

  val someValue = {
    2 < 3
  }
  println(someValue) // true

  val someOtherValue = {
    if (someValue) 239 else 986
    42
  }
  println(someOtherValue) // 42
}
